<template>
  <div class="store-inventory">
    <el-card>
      <template #header>
        <div class="card-header">
          <span class="title">📦 门店库存管理</span>
          <div class="header-actions">
            <el-button type="success" @click="handleStockIn">
              <el-icon><Plus /></el-icon>
              入库
            </el-button>
            <el-button type="warning" @click="handleStockOut">
              <el-icon><Minus /></el-icon>
              出库
            </el-button>
            <el-button type="primary" @click="handleTransfer">
              <el-icon><Switch /></el-icon>
              调拨
            </el-button>
          </div>
        </div>
      </template>

      <!-- 搜索区域 -->
      <el-form :inline="true" class="search-form">
        <el-form-item label="所属门店">
          <el-select v-model="searchForm.storeId" placeholder="请选择门店" clearable>
            <el-option label="深圳旗舰店" value="1" />
            <el-option label="上海陆家嘴店" value="2" />
            <el-option label="北京国贸店" value="3" />
            <el-option label="广州天河店" value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input v-model="searchForm.productName" placeholder="请输入商品名称" clearable />
        </el-form-item>
        <el-form-item label="商品分类">
          <el-select v-model="searchForm.category" placeholder="请选择分类" clearable>
            <el-option label="手机" value="phone" />
            <el-option label="电脑" value="computer" />
            <el-option label="平板" value="tablet" />
            <el-option label="配件" value="accessory" />
          </el-select>
        </el-form-item>
        <el-form-item label="库存状态">
          <el-select v-model="searchForm.stockStatus" placeholder="请选择状态" clearable>
            <el-option label="正常" value="normal" />
            <el-option label="预警" value="warning" />
            <el-option label="缺货" value="shortage" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 库存统计卡片 -->
      <el-row :gutter="20" class="stat-cards">
        <el-col :xs="12" :sm="6">
          <div class="stat-card">
            <div class="stat-icon" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);">
              <el-icon :size="32"><Goods /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-label">商品总数</div>
              <div class="stat-value">{{ totalProducts }}</div>
            </div>
          </div>
        </el-col>
        <el-col :xs="12" :sm="6">
          <div class="stat-card">
            <div class="stat-icon" style="background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);">
              <el-icon :size="32"><Box /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-label">库存总量</div>
              <div class="stat-value">{{ totalStock }}</div>
            </div>
          </div>
        </el-col>
        <el-col :xs="12" :sm="6">
          <div class="stat-card">
            <div class="stat-icon" style="background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);">
              <el-icon :size="32"><Warning /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-label">预警商品</div>
              <div class="stat-value warning">{{ warningProducts }}</div>
            </div>
          </div>
        </el-col>
        <el-col :xs="12" :sm="6">
          <div class="stat-card">
            <div class="stat-icon" style="background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);">
              <el-icon :size="32"><Money /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-label">库存价值</div>
              <div class="stat-value amount">¥{{ (stockValue / 10000).toFixed(1) }}万</div>
            </div>
          </div>
        </el-col>
      </el-row>

      <!-- 库存列表 -->
      <el-table :data="tableData" border stripe>
        <el-table-column prop="productCode" label="商品编码" width="120" />
        <el-table-column prop="productName" label="商品名称" min-width="200">
          <template #default="{ row }">
            <div class="product-cell">
              <el-image 
                :src="row.image" 
                fit="cover" 
                style="width: 40px; height: 40px; border-radius: 4px;"
              />
              <span class="product-name">{{ row.productName }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="category" label="分类" width="100">
          <template #default="{ row }">
            <el-tag size="small">{{ getCategoryName(row.category) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="storeName" label="所属门店" width="150" />
        <el-table-column prop="stock" label="当前库存" width="100" align="right">
          <template #default="{ row }">
            <el-tag :type="getStockTag(row.stock, row.minStock)">
              {{ row.stock }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="minStock" label="安全库存" width="100" align="right" />
        <el-table-column prop="maxStock" label="最大库存" width="100" align="right" />
        <el-table-column prop="price" label="进货价" width="100" align="right">
          <template #default="{ row }">
            ¥{{ row.price.toFixed(2) }}
          </template>
        </el-table-column>
        <el-table-column prop="sellPrice" label="零售价" width="100" align="right">
          <template #default="{ row }">
            ¥{{ row.sellPrice.toFixed(2) }}
          </template>
        </el-table-column>
        <el-table-column prop="stockValue" label="库存价值" width="120" align="right">
          <template #default="{ row }">
            <span class="stock-value">¥{{ (row.stock * row.price).toLocaleString() }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="lastStockInDate" label="最后入库" width="120" />
        <el-table-column label="操作" width="250" fixed="right">
          <template #default="{ row }">
            <el-button link type="primary" @click="handleViewDetail(row)">详情</el-button>
            <el-button link type="success" @click="handleStockInItem(row)">入库</el-button>
            <el-button link type="warning" @click="handleStockOutItem(row)">出库</el-button>
            <el-button link type="info" @click="handleViewLog(row)">日志</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        v-model:current-page="pagination.page"
        v-model:page-size="pagination.size"
        :total="pagination.total"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSearch"
        @current-change="handleSearch"
      />
    </el-card>

    <!-- 入库对话框 -->
    <el-dialog 
      v-model="stockInDialogVisible" 
      title="商品入库"
      width="600px"
    >
      <el-form :model="stockInForm" :rules="stockInRules" ref="stockInFormRef" label-width="100px">
        <el-form-item label="所属门店" prop="storeId">
          <el-select v-model="stockInForm.storeId" placeholder="请选择门店" style="width: 100%">
            <el-option label="深圳旗舰店" value="1" />
            <el-option label="上海陆家嘴店" value="2" />
            <el-option label="北京国贸店" value="3" />
            <el-option label="广州天河店" value="4" />
          </el-select>
        </el-form-item>

        <el-form-item label="选择商品" prop="productId">
          <el-select v-model="stockInForm.productId" placeholder="请选择商品" filterable style="width: 100%">
            <el-option label="iPhone 15 Pro Max" value="1" />
            <el-option label="MacBook Pro 16" value="2" />
            <el-option label="iPad Air" value="3" />
            <el-option label="AirPods Pro" value="4" />
          </el-select>
        </el-form-item>

        <el-form-item label="入库数量" prop="quantity">
          <el-input-number v-model="stockInForm.quantity" :min="1" style="width: 100%" />
        </el-form-item>

        <el-form-item label="进货价格" prop="price">
          <el-input-number v-model="stockInForm.price" :precision="2" :min="0" style="width: 100%" />
        </el-form-item>

        <el-form-item label="供应商">
          <el-select v-model="stockInForm.supplierId" placeholder="请选择供应商" style="width: 100%">
            <el-option label="供应商A" value="1" />
            <el-option label="供应商B" value="2" />
            <el-option label="供应商C" value="3" />
          </el-select>
        </el-form-item>

        <el-form-item label="备注">
          <el-input
            v-model="stockInForm.remark"
            type="textarea"
            :rows="3"
            placeholder="请输入备注信息"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="stockInDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleStockInSubmit">确定</el-button>
      </template>
    </el-dialog>

    <!-- 出库对话框 -->
    <el-dialog 
      v-model="stockOutDialogVisible" 
      title="商品出库"
      width="600px"
    >
      <el-form :model="stockOutForm" :rules="stockOutRules" ref="stockOutFormRef" label-width="100px">
        <el-form-item label="所属门店" prop="storeId">
          <el-select v-model="stockOutForm.storeId" placeholder="请选择门店" style="width: 100%">
            <el-option label="深圳旗舰店" value="1" />
            <el-option label="上海陆家嘴店" value="2" />
            <el-option label="北京国贸店" value="3" />
            <el-option label="广州天河店" value="4" />
          </el-select>
        </el-form-item>

        <el-form-item label="选择商品" prop="productId">
          <el-select v-model="stockOutForm.productId" placeholder="请选择商品" filterable style="width: 100%">
            <el-option label="iPhone 15 Pro Max" value="1" />
            <el-option label="MacBook Pro 16" value="2" />
            <el-option label="iPad Air" value="3" />
            <el-option label="AirPods Pro" value="4" />
          </el-select>
        </el-form-item>

        <el-form-item label="出库数量" prop="quantity">
          <el-input-number v-model="stockOutForm.quantity" :min="1" style="width: 100%" />
        </el-form-item>

        <el-form-item label="出库类型" prop="type">
          <el-select v-model="stockOutForm.type" placeholder="请选择类型" style="width: 100%">
            <el-option label="销售出库" value="sale" />
            <el-option label="调拨出库" value="transfer" />
            <el-option label="退货出库" value="return" />
            <el-option label="损耗出库" value="loss" />
          </el-select>
        </el-form-item>

        <el-form-item label="备注">
          <el-input
            v-model="stockOutForm.remark"
            type="textarea"
            :rows="3"
            placeholder="请输入备注信息"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="stockOutDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleStockOutSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  Plus, Minus, Switch, Goods, Box, Warning, Money
} from '@element-plus/icons-vue'

// 搜索表单
const searchForm = reactive({
  storeId: '',
  productName: '',
  category: '',
  stockStatus: ''
})

// 统计数据
const totalProducts = ref(1856)
const totalStock = ref(12456)
const warningProducts = ref(28)
const stockValue = ref(8567890)

// 表格数据
const tableData = ref([
  {
    id: 1,
    productCode: 'P001',
    productName: 'iPhone 15 Pro Max',
    category: 'phone',
    storeName: '深圳旗舰店',
    stock: 45,
    minStock: 20,
    maxStock: 100,
    price: 8500,
    sellPrice: 9999,
    lastStockInDate: '2024-01-10',
    image: 'https://via.placeholder.com/40'
  },
  {
    id: 2,
    productCode: 'P002',
    productName: 'MacBook Pro 16"',
    category: 'computer',
    storeName: '深圳旗舰店',
    stock: 12,
    minStock: 10,
    maxStock: 50,
    price: 17000,
    sellPrice: 19999,
    lastStockInDate: '2024-01-08',
    image: 'https://via.placeholder.com/40'
  },
  {
    id: 3,
    productCode: 'P003',
    productName: 'iPad Air',
    category: 'tablet',
    storeName: '上海陆家嘴店',
    stock: 8,
    minStock: 15,
    maxStock: 80,
    price: 4200,
    sellPrice: 4999,
    lastStockInDate: '2024-01-05',
    image: 'https://via.placeholder.com/40'
  },
  {
    id: 4,
    productCode: 'P004',
    productName: 'AirPods Pro',
    category: 'accessory',
    storeName: '北京国贸店',
    stock: 67,
    minStock: 30,
    maxStock: 150,
    price: 1700,
    sellPrice: 1999,
    lastStockInDate: '2024-01-12',
    image: 'https://via.placeholder.com/40'
  },
  {
    id: 5,
    productCode: 'P005',
    productName: 'Apple Watch Ultra',
    category: 'accessory',
    storeName: '广州天河店',
    stock: 23,
    minStock: 20,
    maxStock: 60,
    price: 5800,
    sellPrice: 6999,
    lastStockInDate: '2024-01-09',
    image: 'https://via.placeholder.com/40'
  }
])

// 分页
const pagination = reactive({
  page: 1,
  size: 10,
  total: 5
})

// 入库对话框
const stockInDialogVisible = ref(false)
const stockInFormRef = ref(null)
const stockInForm = reactive({
  storeId: '',
  productId: '',
  quantity: 1,
  price: 0,
  supplierId: '',
  remark: ''
})

const stockInRules = {
  storeId: [{ required: true, message: '请选择门店', trigger: 'change' }],
  productId: [{ required: true, message: '请选择商品', trigger: 'change' }],
  quantity: [{ required: true, message: '请输入入库数量', trigger: 'blur' }],
  price: [{ required: true, message: '请输入进货价格', trigger: 'blur' }]
}

// 出库对话框
const stockOutDialogVisible = ref(false)
const stockOutFormRef = ref(null)
const stockOutForm = reactive({
  storeId: '',
  productId: '',
  quantity: 1,
  type: '',
  remark: ''
})

const stockOutRules = {
  storeId: [{ required: true, message: '请选择门店', trigger: 'change' }],
  productId: [{ required: true, message: '请选择商品', trigger: 'change' }],
  quantity: [{ required: true, message: '请输入出库数量', trigger: 'blur' }],
  type: [{ required: true, message: '请选择出库类型', trigger: 'change' }]
}

// 获取分类名称
const getCategoryName = (category) => {
  const map = {
    phone: '手机',
    computer: '电脑',
    tablet: '平板',
    accessory: '配件'
  }
  return map[category] || category
}

// 获取库存标签
const getStockTag = (stock, minStock) => {
  if (stock <= minStock * 0.5) return 'danger'
  if (stock <= minStock) return 'warning'
  return 'success'
}

// 搜索
const handleSearch = () => {
  console.log('搜索:', searchForm)
  ElMessage.success('查询成功')
}

// 重置
const handleReset = () => {
  searchForm.storeId = ''
  searchForm.productName = ''
  searchForm.category = ''
  searchForm.stockStatus = ''
  handleSearch()
}

// 入库
const handleStockIn = () => {
  stockInDialogVisible.value = true
}

const handleStockInItem = (row) => {
  stockInForm.storeId = row.storeId
  stockInForm.productId = row.id
  stockInDialogVisible.value = true
}

const handleStockInSubmit = () => {
  stockInFormRef.value.validate((valid) => {
    if (valid) {
      console.log('入库:', stockInForm)
      ElMessage.success('入库成功')
      stockInDialogVisible.value = false
      handleSearch()
    }
  })
}

// 出库
const handleStockOut = () => {
  stockOutDialogVisible.value = true
}

const handleStockOutItem = (row) => {
  stockOutForm.storeId = row.storeId
  stockOutForm.productId = row.id
  stockOutDialogVisible.value = true
}

const handleStockOutSubmit = () => {
  stockOutFormRef.value.validate((valid) => {
    if (valid) {
      console.log('出库:', stockOutForm)
      ElMessage.success('出库成功')
      stockOutDialogVisible.value = false
      handleSearch()
    }
  })
}

// 调拨
const handleTransfer = () => {
  ElMessage.info('库存调拨功能开发中')
}

// 查看详情
const handleViewDetail = (row) => {
  ElMessage.info(`查看商品：${row.productName}`)
}

// 查看日志
const handleViewLog = (row) => {
  ElMessage.info(`查看 ${row.productName} 的库存日志`)
}
</script>

<style scoped>
.store-inventory {
  padding: 20px;
  background: #f5f5f5;
  min-height: 100vh;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title {
  font-size: 18px;
  font-weight: bold;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.search-form {
  margin-bottom: 20px;
}

/* 统计卡片 */
.stat-cards {
  margin-bottom: 20px;
}

.stat-card {
  display: flex;
  align-items: center;
  padding: 20px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
  transition: transform 0.3s;
}

.stat-card:hover {
  transform: translateY(-5px);
}

.stat-icon {
  width: 60px;
  height: 60px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  margin-right: 15px;
}

.stat-content {
  flex: 1;
}

.stat-label {
  font-size: 14px;
  color: #909399;
  margin-bottom: 8px;
}

.stat-value {
  font-size: 24px;
  font-weight: bold;
  color: #303133;
}

.stat-value.warning {
  color: #f56c6c;
}

.stat-value.amount {
  color: #409eff;
  font-size: 20px;
}

/* 商品单元格 */
.product-cell {
  display: flex;
  align-items: center;
  gap: 10px;
}

.product-name {
  font-weight: 500;
}

.stock-value {
  color: #409eff;
  font-weight: bold;
}

.el-pagination {
  margin-top: 20px;
  justify-content: flex-end;
}
</style>

